package com.shop.technicalservice.dao.user.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shop.technicalservice.dao.user.UserinfoDao;
import com.shop.technicalservice.model.user.Userinfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * Userinfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see org.rjb.pojo.UserinfoDto
 * @author MyEclipse Persistence Tools
 */

public class UserinfoDaoImpl extends HibernateDaoSupport implements  UserinfoDao{
	private static final Log log = LogFactory.getLog(UserinfoDaoImpl.class);
	// property constants
	public static final String USER_NAME = "userName";
	public static final String SEX = "sex";
	public static final String PROVINCE = "province";
	public static final String CITY = "city";
	public static final String PHONE = "phone";
	public static final String MOBILE = "mobile";
	public static final String REAL_NAME = "realName";
	public static final String ADDRES = "addres";
	public static final String POST_CODE = "postCode";
	public static final String EMAIL = "email";
	public static final String UCODE_TYPE = "ucodeType";
	public static final String UCODE_NUMBER = "ucodeNumber";

	protected void initDao() {
		// do nothing
	}

	public Userinfo save(Userinfo transientInstance) throws Exception{
		log.debug("saving Userinfo instance");
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		return transientInstance;
	}

	public void delete(Userinfo persistentInstance) throws Exception{
		log.debug("deleting Userinfo instance");
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
	}

	public Userinfo findById(java.lang.Integer id) throws Exception{
		log.debug("getting Userinfo instance with id: " + id);
			Userinfo instance = (Userinfo) getHibernateTemplate().get(
					"org.rjb.Userinfo", id);
			return instance;
	}

	public List findByExample(Userinfo instance) throws Exception{
		log.debug("finding Userinfo instance by example");
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
	}

	public List findByProperty(String propertyName, Object value) throws Exception{
		log.debug("finding Userinfo instance with property: " + propertyName
				+ ", value: " + value);
			String queryString = "from Userinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
	}

	public List findByUserName(Object userName) throws Exception{
		return findByProperty(USER_NAME, userName);
	}

	public List findBySex(Object sex) throws Exception{
		return findByProperty(SEX, sex);
	}

	public List findByProvince(Object province)throws Exception{
		return findByProperty(PROVINCE, province);
	}

	public List findByCity(Object city) throws Exception{
		return findByProperty(CITY, city);
	}

	public List findByPhone(Object phone) throws Exception{
		return findByProperty(PHONE, phone);
	}

	public List findByMobile(Object mobile) throws Exception{
		return findByProperty(MOBILE, mobile);
	}

	public List findByRealName(Object realName) throws Exception{
		return findByProperty(REAL_NAME, realName);
	}

	public List findByAddres(Object addres) throws Exception{
		return findByProperty(ADDRES, addres);
	}

	public List findByPostCode(Object postCode) throws Exception{
		return findByProperty(POST_CODE, postCode);
	}

	public List findByEmail(Object email) throws Exception{
		return findByProperty(EMAIL, email);
	}

	public List findByUcodeType(Object ucodeType) throws Exception{
		return findByProperty(UCODE_TYPE, ucodeType);
	}

	public List findByUcodeNumber(Object ucodeNumber) throws Exception{
		return findByProperty(UCODE_NUMBER, ucodeNumber);
	}

	public List findAll() throws Exception{
		log.debug("finding all Userinfo instances");
			String queryString = "from Userinfo";
			return getHibernateTemplate().find(queryString);
	}

	public Userinfo merge(Userinfo detachedInstance) throws Exception{
		log.debug("merging Userinfo instance");
			Userinfo result = (Userinfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
	}

	public void attachDirty(Userinfo instance) throws Exception{
		log.debug("attaching dirty Userinfo instance");
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
	}

	public void attachClean(Userinfo instance) throws Exception{
		log.debug("attaching clean Userinfo instance");
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
	}

	public static UserinfoDaoImpl getFromApplicationContext(ApplicationContext ctx) {
		return (UserinfoDaoImpl) ctx.getBean("UserinfoDAO");
	}
}